package se.liu.johfa428.game.levels.level00;

import se.liu.johfa428.game.objects.levelselection.ObjectLevelSelectionCastle;
import se.liu.johfa428.game.objects.levelselection.ObjectLevelSelectionInvisibleBlock;
import se.liu.johfa428.game.objects.levelselection.ObjectLevelSelectionMenu;
import se.liu.johfa428.game.objects.levelselection.ObjectLevelSelectionMountain;
import se.liu.johfa428.game.objects.levelselection.ObjectLevelSelectionNode;
import se.liu.johfa428.game.objects.levelselection.ObjectLevelSelectionPlatform;
import se.liu.johfa428.game.objects.levelselection.ObjectLevelSelectionPlayer;
import se.liu.johfa428.game.objects.levelselection.ObjectLevelSelectionRoad;
import se.liu.johfa428.game.objects.levelselection.ObjectLevelSelectionStatusPanel;
import se.liu.johfa428.game.objects.levelselection.ObjectLevelSelectionTree;
import se.liu.johfa428.levelstate.LevelState;
import se.liu.johfa428.levelstate.LevelState.State;
import se.liu.oschi129.level.levelspace.LevelInterface;
import se.liu.oschi129.level.levelspace.LevelSpace;
import se.liu.oschi129.world.model.World;
import se.liu.oschi129.world.model.WorldPhysicalRules;
import se.liu.oschi129.world.model.WorldView;

public class Level00_LevelSelection  implements LevelInterface{
	
	double dimY = 46/2;
	double dimX = 57/2;
	
	public boolean placeTree(double i, double j){		
		return (j == 0 || (j==1 && (i == 0 || (3 <= i && 4 >= i) || i == 9))
				|| (j == 2 && i == 9) || (j == 3 ||  j == 4) && (i == 0 || i == 5))
				||  (j == 5 && i == 9) ||  (j == 6 && i >= 8) ;
	}

	public boolean placeRock(double i, double j){
		return((j == 1 && i == 5) || (i == 4 && (j >= 2 && j <= 4)) || (i == 5 && j == 2) ||
				(i == 3 && j== 3) || ((i == 3 || i == 4) && (j == 6))) ;
	}
	
	public boolean placeSolidBlock(double i, double j){
		return((i == 1 && j == 0)  || ((i == 0 || i == 2 || i == 7) & j == 1) ||
				((i == 6 || i == 8) && j == 2) || ((i == 0 || i == 8) && (j == 5)) ||
						(i == 1 || i == 7) && j == 6);
	}
	
	public void createWorld(World world, LevelState levelState) {
		
		ObjectLevelSelectionTree tree;
		ObjectLevelSelectionRoad roade;
		ObjectLevelSelectionMountain rock;
		ObjectLevelSelectionPlatform platform;
		ObjectLevelSelectionInvisibleBlock block;		
		ObjectLevelSelectionPlayer character;
		ObjectLevelSelectionCastle castle;
		ObjectLevelSelectionNode node;
		LevelState state;
		
		world.setView(new WorldView(320, 240));
		world.setWidth(320);
		world.setHeight(240);
		world.setCoins(0);
		world.setPoints(0);
		world.setLives(3);
		
		world.setPhysicalRules(new WorldPhysicalRules());
		world.addObjectSynchronized(new ObjectLevelSelectionMenu());
		world.addObjectSynchronized(new ObjectLevelSelectionStatusPanel(world));
		
		for(int i = 0; i <= 9; i++){ 
			for (int j = 0; j<= 6; j++){						

				if (placeTree(i, j)){
					tree  = new ObjectLevelSelectionTree();	
					world.addObjectSynchronized(tree, 13+i*dimX, 18+j*dimY);				
				}

				if (placeRock(i, j)){
					rock = new ObjectLevelSelectionMountain();
					world.addObjectSynchronized(rock, 13+i*dimX, 18+j*dimY);					
				}
				
				if (i == 1 && (j >= 2 && j <= 4) || (i == 7 && (j == 3 || j == 4))) {
					roade = new ObjectLevelSelectionRoad(0);
					world.addObjectSynchronized(roade,dimX*i + 13, dimY*j+18);
				}
				
				if ((2 <= i && i <= 6) && j == 5){
					roade = new ObjectLevelSelectionRoad(1);
					world.addObjectSynchronized(roade,dimX*i + 13, dimY*j+18);
				}
				
				// places nodes
				if ((i == 1 && j == 3) || (i == 4 && j == 5)){
					node = new ObjectLevelSelectionNode();
					node.centerPos(dimX, dimY);
					world.addObjectSynchronized(node,dimX*i + 13+node.getX(), dimY*j+18+node.getY());
				}
				
				// places solids blocks that are invisible
				if(placeSolidBlock(i, j)){
					block = new ObjectLevelSelectionInvisibleBlock(0,0);
					world.addObjectSynchronized(block,dimX*i + 13, dimY*j+18);
				}
			}
		}
				
		//Starting position
		state = new LevelState(State.START);
		platform = new ObjectLevelSelectionPlatform(0, 0, null, state, 0);
		world.addInteractiveObjectSynchronized(platform);
		world.addObjectSynchronized(platform,dimX*1 + 13, dimY*1+18);

		character = new ObjectLevelSelectionPlayer(0, 0);
		world.addInteractiveObjectSynchronized(character);
		world.addObjectSynchronized(character,dimX*1 + 13, dimY*1+18);
				
		//Position for Level 1
		state = new LevelState(State.NORMAL);
		platform = new ObjectLevelSelectionPlatform(0, 0, LevelSpace.get("level01"), state, 1);
		world.addInteractiveObjectSynchronized(platform);
		world.addObjectSynchronized(platform,dimX*1 + 13, dimY*5+18);
		
		// places solids blocks that are invisible and will disappear when level 01 i cleared
		block = new ObjectLevelSelectionInvisibleBlock(0,0,state);
		world.addObjectSynchronized(block,dimX*2 + 13, dimY*5+18);

		//Position for Level 2
		state = new LevelState(State.NORMAL);
		platform = new ObjectLevelSelectionPlatform(0, 0, LevelSpace.get("level02"), state, 2);
		world.addInteractiveObjectSynchronized(platform);
		world.addObjectSynchronized(platform,dimX*7 + 13, dimY*5+18);
		
		// places solids blocks that are invisible and will disappear when level 02 i cleared
		block = new ObjectLevelSelectionInvisibleBlock(0,0,state);
		world.addObjectSynchronized(block,dimX*7 + 13, dimY*4+18);
		
		//places castle
		castle = new ObjectLevelSelectionCastle();
		//castle.centerPos(dimX, dimY);
		world.addInteractiveObjectSynchronized(castle);
		world.addObjectSynchronized(castle, dimX*7 + 13+castle.getX(), dimY*2+18+castle.getY()-12);
		
	}

}
